#include"LContact.h"
#include"SList.h"

//初始化通讯录
void InitContact(contact** con)
{
    assert(con);
    *con=NULL;
}
//添加联系人
void AddContact(contact** con)
{
    assert(con);
    PeoInfo info;
    printf ("请输入联系人姓名：\n");
    scanf ("%s",info.name);
    printf ("请输入联系人姓别：\n");
    scanf ("%s",info.sex);
    printf ("请输入联系人年龄：\n");
    scanf ("%d",&info.age);
    printf ("请输入联系人电话：\n");
    scanf ("%s",info.tel);
    printf ("请输入联系人地址：\n");
    scanf ("%s",info.addr);
    //头插
    SLPushFront(con,info);
}
//通过姓名查找
contact* FindByName (contact* pcon,char name[])
{
    if(pcon==NULL)
        return NULL;
    do
    {
        if(strcmp(pcon->data.name,name)==0)
            return pcon;
        pcon=pcon->next;
    }while(pcon);
    return NULL;
}
//删除联系人
void DelContact(contact** con)
{
    assert(con&&*con);
    //强制要求用姓名查找
    printf ("请输入要删除的联系人姓名：\n");
    char name[NAME_MAX];
    scanf("%s",name);
    contact* find=FindByName(*con,name);
    if(find==NULL)
    {
        printf("查无此人！\n");
        return ;
    }
    SLErase(con,find);
}
//展示通讯录数据
void ShowContact(contact** con)
{
    assert(con);
    contact* cur=*con;
    if(cur==NULL)
    {
        printf("通讯录为空！\n");
        return;
    }
    //表头
    printf ("%s %s %s %s %s\n","姓名","性别","年龄","电话","住址");
    while(cur!=NULL)
    {
        printf (" %-4s %-4s %-4d %-4s %-4s\n",
            cur->data.name,
            cur->data.sex,
            cur->data.age,
            cur->data.tel,
            cur->data.addr);
        cur=cur->next;
    }
    printf("\n");
}
//查找联系人
void FindContact(contact** con)
{
    assert(con&&*con);
    printf ("请输入要查找的联系人姓名：\n");
    char name[NAME_MAX];
    scanf("%s",name);
    contact* find=FindByName(*con,name);
    if(find==NULL)
    {
        printf("查无此人！\n");
        return ;
    }
    printf ("%s %s %s %s %s\n","姓名","性别","年龄","电话","住址");
    printf (" %-4s %-4s %-4d %-4s %-4s\n",
            find->data.name,
            find->data.sex,
            find->data.age,
            find->data.tel,
            find->data.addr);
}
//修改联系人
void ModifyContact(contact** con)
{
    assert(con);
    printf ("请输入要修改的联系人姓名：\n");
    char name[NAME_MAX];
    scanf("%s",name);
    contact* find=FindByName(*con,name);
    if(find==NULL)
    {
        printf("查无此人！\n");
        return ;
    }
    printf ("请输入新的联系人姓名：\n");
    scanf ("%s",find->data.name);
    printf ("请输入新的联系人姓别：\n");
    scanf ("%s",find->data.sex);
    printf ("请输入新的联系人年龄：\n");
    scanf ("%d",&find->data.age);
    printf ("请输入新的联系人电话：\n");
    scanf ("%s",find->data.tel);
    printf ("请输入新的联系人地址：\n");
    scanf ("%s",find->data.addr);
}
//销毁通讯录数据
void DestroyContact(contact** con)
{
    SLDesTroy(con);
}
 